$(function () {
    // getCodeData();
    getCode();
    getUserList();
});
//为当前显示条数文本框绑定回车事件
$('#size').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        //敲击回车后重新按照新的显示条数加载页面
        getUserList();
    }
});

//为当前页数文本框绑定回车事件
$('#pageipt').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        getUserList();
    }
});
var total = 0;
var index = 1;
var pageSize = 5;
//为上一页绑定点击事件
$('#lastpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    page--;
    if (page >= index) {//如果不是第一页，则往前翻一页
        $("#pageipt").val(page);
        getUserList();
    }
});

//绑定下一页点击事件
$('#nextpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    let size = $("#size").val();
    page++;
    if (page <= Math.ceil(total / size)) {
        $("#pageipt").val(page);
        getUserList();
    }
});

//为首页绑定点击事件
$("#first").click(function () {
    let page = $("#pageipt").val();
    if (page > index) {//如果当前页不是首页则跳转到首页，否则不做任何事情
        page = index;
        $("#pageipt").val(page);
        getUserList();
    }
});

//为尾页绑定点击事件
$("#last").click(function () {
    let page = $("#pageipt").val();
    let size = $("#size").val();
    if (page < Math.ceil(total / size)) {//如果当前页不是尾页，则跳转到尾页
        page = Math.ceil(total / size);
        $("#pageipt").val(page);
        getUserList();
    }
});

function getUserList() {
    let name = $("#name").val();
    let start_date = $("#start_date").val();
    let end_date = $("#end_date").val();
    let team = $("#team_select").find("option:selected").val();
    let sort = $("#sort").find("option:selected").val();
    let status = $("#status_select").find("option:selected").val();
    let gender = $("#gender_select").find("option:selected").val();
    let degree = $("#degree_select").find("option:selected").val();
    let role = $("#role_select").find("option:selected").val();
    let page = $("#pageipt").val();
    let size = $("#size").val();
    if (page.trim() == '' || parseInt(page.trim()) < index)
        page = index;
    if (size.trim() == '' || parseInt(size.trim()) < index) {
        size = pageSize;
    }
    let map = {
        "name": name,
        "start_date": start_date,
        "end_date": end_date,
        "team": team,
        "sort": sort,
        "status": status,
        "gender": gender,
        "degree": degree,
        "role": role,
        "page": page,
        "size": size
    };
    let data = myAjax("/user/getUserList", "post", map, "json");
    console.log(data);
    $("tbody").html("");
    if (data.code == 200) {
        $.each(data.list, function (i, user) {
            $("tbody").append("<tr><td><input type='checkbox' value='" + user.id + "'></td><td>" + user.id + "</td><td>" + user.username + "</td>" +
                "<td>" + avoidNull(user.name) + "</td><td>" + avoidNull(user.gender.name) + "</td><td>" + avoidNull(user.age) + "</td><td>" + (user.team == null ? "暂未分组" : user.team.name) + "</td>" +
                "<td>" + (user.degree == null ? "暂未选择" : user.degree.name) + "</td><td>" + avoidNull(user.graduate) + "</td><td>" + (user.nativePlace == null ? "待完善" : user.nativePlace) + "</td>" +
                "<td>" + avoidNull(user.address) + "</td><td>" + avoidNull(user.phone) + "</td><td>" + avoidNull(user.emergencyPhone) + "</td>" +
                "<td>" + user.createTime + "</td><td>" + (user.updateTime == null ? "暂无更新" : user.updateTime) + "</td>" +
                "<td>" + setStatus(user.id, JSON.stringify(user.status.code)) + "</td></tr>");
        });
        total = data.total;
        //显示总条数
        $("#pagecount").text(total);
    }
}

function avoidNull(data) {
    if (data == null)
        data = "待完善";
    return data;
}

function getCode() {
    if (sessionStorage.getItem("gender") == null)
        getData();
    let gender = JSON.parse(sessionStorage.getItem("gender"));
    $("#gender_select").html("<option value='全部'>全部</option>");
    $.each(gender, function (i, option) {
        $("#gender_select").append("<option value=" + option.code + ">" + option.name + "</option>");
    });
    let degree = JSON.parse(sessionStorage.getItem("degree"));
    $("#degree_select").html("<option value='全部'>全部</option>");
    $.each(degree, function (i, option) {
        $("#degree_select").append("<option value=" + option.code + ">" + option.name + "</option>");
    });
    let status = JSON.parse(sessionStorage.getItem("status"));
    $("#status_select").html("<option value='全部'>全部</option>");
    $.each(status, function (i, option) {
        $("#status_select").append("<option value=" + option.code + ">" + option.name + "</option>");
    });
    let role = JSON.parse(sessionStorage.getItem("role"));
    $("#role_select").html("<option value='全部'>全部</option>");
    $.each(role, function (i, option) {
        $("#role_select").append("<option value=" + option.code + ">" + option.name + "</option>");
    });
    let team = JSON.parse(sessionStorage.getItem("team"));
    $("#team_select").html("<option value='全部'>全部</option>");
    $.each(team, function (i, option) {
        $("#team_select").append("<option value=" + option.code + ">" + option.name + "</option>");
    });
}

function setStatus(id, status) {
    if (JSON.parse(status) == "normal")
        return "<button onclick='updateStatus(" + id + "," + JSON.stringify("frozen") + ")'>正常</button>";
    else if (JSON.parse(status) == "frozen")
        return "<button onclick='updateStatus(" + id + "," + JSON.stringify("normal") + ")'>冻结</button>";
    else
        return "错误";
}

function updateStatus(id, status) {
    let data = myAjax("/user/updateStatus", "post", {"id": id, "status": status}, "json");
    if (data == 200)
        getUserList();
}
function deleteSelected() {
    let usersid = new Array();
    $("input[type=checkbox]:checked").each(function (key, value) {
        if ($(this).val() != "on")
            usersid.push(parseInt($(this).val()));
    });
    console.log(usersid);
    let data = myAjax("/user/deleteUsers","post",{"arrays":JSON.stringify(usersid)},"json");
    if (data==200)
        alert("删除成功");
    else if (data==500)
        alert("后台错误");
    else
        alert("404未找到");
    getUserList();
}